Skip to content

Make LowIR one language and compile each object identically from its text - #174

Merged
vishvananda merged 8 commits into
perf/reuse-release-library-objectsfrom
fix/lowir-one-language
Nov 16, 2026
Merged

vishvananda merged 8 commits into
perf/reuse-release-library-objectsfrom
fix/lowir-one-language

Conversation

@vishvananda

@vishvananda vishvananda commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

This branch is on top of #173 and ends with the two commits of #175, so the
three can be fast-forwarded together.

One LowIR language

There were two LowIR dialects. The host-object classes (x87, complex_x87,
sseup, memory results, classes= on variadic call arguments and on va_arg)
were read only when a program came from the object compile, and lowiropt
rejected them. A program that lowiropt read and wrote could then not be
compiled to the same object.

These facts are necessary: the native objects need them for long double,
__int128, vector and large object boundaries, and the Wasm objects use the
same fields. They are now part of the contract (pa8/lowir.md "Object ABI
Conventions" and "Variadic Argument Access"):

  • Every reader accepts them. Program::object_facts and the numeric class
    spelling are removed.
  • One rule, valid_object_classes, says which classes a type can have. The
    validator checks argument and va_arg classes against the real operand.
  • PA8 has fixtures for the accepted forms and for each rejected one. PA24 has
    a behavior fixture: 12 checks through pointer calls and variadic boundaries.
  • The PA33 object round trip now compiles the text after lowiropt -O0 has
    read and written it, so lowiropt must keep every fact.

Two PA8 fixtures change: 300-bad-classes-unknown now uses an unknown word,
and 300-bad-result-classes-memory is removed because a memory result is now
valid. No other references change in that commit; commit 6 changes one
(below).

Commits

  1. Choose block lane runs independently of how the host sorts repeated
    addresses. The SIMD difference between the GCC-made and the Clang-made
    lowiropt came from std::sort, which is not stable: the two standard
    libraries put stores to one address in different orders. New PA33 fixture:
    o3/500-lanes-of-a-copy-written-again.t.

  2. Make host object classes part of the LowIR contract (above).

  3. Leave x86-64 classes off soft-float helpers on Wasm targets. Wasm objects
    do not change.

  4. Give the runtime unroller's group loops a block kind of their own. A full
    unroll gave its copy of a plain block the kind Unrolled, and the runtime
    unroller reads Unrolled as its own group loop. A block read from text is
    always plain, so at O3 the same source unrolled differently from text and
    from memory (x86 and Wasm). The group header now has the kind
    UnrolledGroup, which prints as unrolled_N as before. The kind is added
    at the end of the shipped lowir_local_model.h enum. New PA33 fixture:
    500-o3-unrolled-copy-as-loop-header.cpp.

  5. Compile Wasm objects identically from their LowIR text. The Wasm back end
    depended on in-memory ids and order. Now:

    • symbols follow the written order;
    • types are numbered by first reference;
    • dispatch and setjmp cases are numbered by layout position;
    • locals are given out in definition order, and a temporary that no placed
      instruction defines gets no local;
    • the LSDA section is named by the object name;
    • -c and the --emit-lowir -g0 object half use the same lowering mode.

    check_wasm.pl now compares, at each level, the object compiled from the
    object LowIR (after lowiropt -O0) with the direct object byte for byte.
    The PA37 README states this. The PA33 round-trip script takes --target.
    PA37 and PA38 (whose test-wasm runs the same checker) now build
    lowiropt, as does the WASI reference export job.

  6. Export the optimized LowIR of an object compile with -S -emit-lowir
    (below).

  7. and 8. The two commits of Run and publish the complete WASI self-build on dedicated Intel #175, unchanged: find Intel dispatches by unique
    runner identity, and run and publish the full WASI self-build on dedicated
    Intel.

Optimized LowIR from the driver

cppgm -S -emit-lowir writes the LowIR that -c compiles, after the LowIR
optimizer, as clang's -S -emit-llvm does. -fno-lowir-passes compiles
without the LowIR optimizer; the back end still runs at the -O level:

cppgm -O2 -S -emit-lowir x.cpp -o x.lowir
cppgm -O2 -c x.cpp -o x.o
cppgm -O2 -c -fno-lowir-passes x.lowir -o y.o   # y.o is x.o byte for byte
  • It works for native and Wasm targets, and for LowIR inputs.
  • The old form of the object LowIR (--emit-lowir with a debug level and a
    .lowir output) now runs this driver path, so there is one producer. The
    PA33 driver lane references do not change. For a Wasm target with
    -gline-tables-only, that text now has no debug locations, as -c lowers
    none for Wasm.
  • These flags belong to this solution, not to the course contract. --help
    lists them after the course flags; the shipped help text does not change.

To give the same object from the text, three things changed:

  • The text reader numbers temporaries in definition order, as the
    optimizer's compaction leaves them. Before, it numbered a temporary where
    the text first names it, which for a phi operand on a back edge is before
    its definition, and the native register allocator then broke ties
    differently. Every reader of LowIR text (-c x.lowir, lowiropt, lowir)
    takes this one route.
  • Wasm gives no type to a signature that nothing refers to. The optimizer can
    remove the last call of a signature, and the text does not write it.
  • Wasm also puts symbols in the written order after the optimizer, which adds
    symbols such as switch tables.

Direct Wasm objects can change in the last two cases. Native objects do not
change.

lowiropt orders commutative operands by temporary number, so its output
changes where the numbering moved. One reference changes, regenerated with
make ref-test: pa33/tests/o1/200-separate-latch-counter.ref now writes
binary add i64 %sum, %next for %next, %sum. This is presentation, which
the course comparison absorbs, and the fixture's property (where the step
may move) is unchanged. The renumbering stays in the reader rather than in
the object path alone, so that all readers take one route. Its cost on a
5.4 MB optimized text from an LLVM unit, in instructions: lowiropt -O0 +0.8%
(read and write only), lowiropt -O2 +0.15%, cppgm -O2 -c x.lowir +0.1%.
Source compiles do not read text.

The tests are solution lanes, and the export prunes them:

  • PA33's maintainer/Makefile turns on an optimized-text check in the object
    round trip: native, with and without debug information, O0 to O3: 52/52.
  • PA37's maintainer/Makefile turns on the same check in check_wasm.pl for
    each fixture and level. A harness test covers the check.
  • The PA33 round-trip fixtures for both Wasm targets: 0 differences.

Checks

  • CPPGM_LOWIR_DIRECT_TEXT_COMPARE=1 make test-report: 11320/11320
  • make test-debuginfo, make -C pa35 test-through-pa5,
    make -C pa36 test-smoke, make test-harness: pass
  • PA37 with the new byte check: 225/225. A sweep of all PA37 fixtures for
    both Wasm targets at O0 to O3: 0 differences. PA38 test-wasm (C
    fixtures): 105/105.
  • A local student export regenerates every reference to the tracked text.
  • All audits pass.
  • Compile time, on 16 LLVM units against the exact base of each change:
    instructions identical at O0, O2 and O3 (ratio 1.0000). Cycles are within
    noise (least-of ratios 1.004, 0.998 and 1.004 for commits 1–5, and 1.003,
    1.003 and 1.005 for commit 6; an O3 A/A run gives 1.001). The native objects
    of those units are identical (O2 and O3 for commits 1–5; O0, O2 and O3 for
    commit 6).
  • PA37 test time: +7.6% from the byte check of commit 5. The solution lanes
    for commit 6 add more time in this repository only.

🤖 Generated with Claude Code

@vishvananda
vishvananda force-pushed the fix/lowir-one-language branch from 8b23bdb to 476eb53 Compare November 16, 2026 21:38
@vishvananda
vishvananda merged commit 476eb53 into perf/reuse-release-library-objects Nov 16, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant